package com.xiaochong.tir.api.util;

import com.sun.org.apache.bcel.internal.generic.NEW;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/4/27.
 */
public  class StringUtil {

    //保留小数点后一位
    public static Double getDoubleOne(Double dou){
        String b=String.format("%.1f", dou);
        Double c=Double.parseDouble(b);
        return c;
    }

    //保留小数点后两位位
    public static Double getDoubleTwo(Double dou){
        String b=String.format("%.2f", dou);
        Double c=Double.parseDouble(b);
        return c;
    }

    public static String getLastHour(){
        Calendar calendar = Calendar.getInstance();
/* HOUR_OF_DAY 指示一天中的小时 */
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
        SimpleDateFormat df = new SimpleDateFormat("HH");
//        System.out.println("一个小时前的时间：" + df.format(calendar.getTime()));
//        System.out.println("当前的时间：" + df.format(new Date()));
        return df.format(calendar.getTime());
    }


    public static double getPriceTwo(double f) {
        BigDecimal bg = new BigDecimal(f);
        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return  f1;
    }


    public static Object pageBean(List datas, int pageIndex, int pageSize){
        pageIndex=pageIndex-1;

        Integer PAGE_SIZE = pageSize;
//        List<Object> datas = Arrays.asList(new Long [] {1L,2L,3L,4L,5L,6L,7L,8L});
        //总记录数
        Integer totalCount = datas.size();
        //分多少次处理
        if(totalCount>0){
            Integer requestCount = totalCount / PAGE_SIZE;

            if (requestCount<1){
                requestCount=1;
            }
            List<Object> list=new ArrayList<>(  );
            for (int i = 0; i <= requestCount; i++) {
                Integer fromIndex = i * PAGE_SIZE;
                //如果总数少于PAGE_SIZE,为了防止数组越界,toIndex直接使用totalCount即可
                int toIndex = Math.min(totalCount, (i + 1) * PAGE_SIZE);
                List subList = datas.subList(fromIndex, toIndex);
                list.add( subList );
//            System.out.println(subList);
                //总数不到一页或者刚好等于一页的时候,只需要处理一次就可以退出for循环了
                if (toIndex == totalCount) {
                    break;
                }
            }
//        System.out.println(JSON.toJSONString( list.get( pageIndex ) ));
            return list.get( pageIndex );
        }
        return null;
    }



    //保留小数点后几位小数
    public static Double RetainCount (Double f,int count){
        BigDecimal bigDecimal = new BigDecimal(f);
//这里的 2 就是你要保留几位小数。
        double f1 = bigDecimal.setScale(count, BigDecimal.ROUND_HALF_UP).doubleValue();
//        System.out.println("保留两位小数 : " + f1);
        return f1;
}

    public static void main(String[] args) {
        Integer requestCount = 10 / 20;
        System.out.println(requestCount);
    }

}
